package org.example.listenerorder.config;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.example.listenerorder.model.OrderVO;
import org.example.listenerorder.task.printOrder;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;

import javax.print.PrintException;
import javax.print.PrintService;
import java.awt.print.PrinterException;
import java.io.IOException;
import java.io.InputStream;

@Slf4j
public class RedisMessageListener implements MessageListener {
    @Override
    public void onMessage(Message message, byte[] pattern) {


        OrderVO orderVO = JSONObject.parseObject(message.getBody(), OrderVO.class);
        System.out.println(orderVO);
        try {
            InputStream stream = printOrder.renderTemplate(orderVO);
            PrintService printer = printOrder.getPrinter();
            if (printer != null) {
                printOrder.spirePrint(stream,printer);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
